package cn.yeamin.ms.controller.admin.system;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import cn.yeamin.ms.commons.JsonResult;
import cn.yeamin.ms.commons.PaginationResult;
import cn.yeamin.ms.controller.support.BaseController;
import cn.yeamin.ms.service.impl.RoleService;
import cn.yeamin.ms.utils.IsEmptyUtils;
import cn.yeamin.ms.vo.Role;
import cn.yeamin.ms.vo.User;

/**
 * @packageName: 包名: cn.yeamin.ms.controller.admin.system
 * @classDesc: 类功能描述:()
 * @author: Administrator
 * @createTime: 2017/7/28 9:48
 * @version: v1.0
 * @copyright:奕明(上海)影视传媒有限公司
 */
@Controller
@RequestMapping("/admin/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @GetMapping(value = { "/", "/index" })
    public String index() {
        return "admin/role/index";
    }

    @PostMapping(value = { "/list" })
    @ResponseBody
    public PaginationResult list(
            @RequestParam(value="pageSize",defaultValue = "3") Integer pageSize,
            @RequestParam(value="pageNumber",defaultValue = "1") Integer pageNumber,
            @RequestParam(value="searchText",required=false) String searchText,
            @RequestParam(value="sortOrder",defaultValue = "ASC") String sortOrder) {
    	 List<String> property = null;
    	 if(!IsEmptyUtils.isEmptyForStr(searchText)) {
    		    property = new ArrayList<String>();
    	    	property.add("name");
    	    	property.add("roleKey");
    	 }
        PageInfo<Role> info = roleService.queryPageListOrderBy(pageNumber,pageSize,Role.class,"create_time " +sortOrder,searchText,property);
        return new PaginationResult(info.getTotal(),info.getList());
    }
    
	@GetMapping(value = "/add")
	public String add() {
		return "admin/role/form";
	}
    
    
    @PostMapping("/edit")
    @ResponseBody
    public JsonResult edit(Role role){
        try {
        	User loginUser = getCurrentUser();
            if (role != null && role.getId() == null) {
            	role.setStatus(1);
            	role.setCreatedBy(loginUser.getUserName());
            	role.setUpdatedBy(loginUser.getUserName());
            	roleService.save(role);
            } else {
            	role.setCreatedBy(null);
            	role.setUpdatedBy(loginUser.getUserName());
            	roleService.updateSelective(role);
            }
            return JsonResult.success();
        }catch (Exception e) {
        	return JsonResult.failure("服务器异常,请稍后操作!");
        }
    }
    
    
    @GetMapping(value = "/edit/{id}")
	public String edit(@PathVariable Long id,ModelMap map) {
		Role role = roleService.queryById(id);
		map.put("role", role);
		return "admin/role/form";
	}

    
    @GetMapping("/grant/{id}")
	public String grant(@PathVariable Long id, ModelMap map) {
		Role role = roleService.queryById(id);
		map.put("role", role);
		return "admin/role/grant";
	}

	@PostMapping("/grant/{id}")
	@ResponseBody
	public JsonResult grant(@PathVariable Long id,
			@RequestParam(required = false) Long[] resourceIds, ModelMap map) {
		try {
			roleService.grantResource(id,resourceIds);
		}catch (IllegalStateException e) {
        	return JsonResult.failure(e.getMessage());
        }catch (Exception e) {
        	e.printStackTrace();
        	return JsonResult.failure("服务器异常,请稍后操作!");
        }
		return JsonResult.success();
	}
	
	@PostMapping(value = "/delete/{id}")
	@ResponseBody
	public JsonResult delete(@PathVariable Long id) {
		try {
			roleService.deleteById(id);
		} catch (Exception e) {
			e.printStackTrace();
			JsonResult.failure("服务器异常,请稍后操作!");
		}
		return JsonResult.success();
	}
}
